package com.locki.execDDL.ui;

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import com.locki.execDDL.service.ExecDDL;
import com.locki.execDDL.service.OpenUrl;

public class MainWin {

	private JFrame frame;
	private JTextField dsField;
	private JTextField scriptField;
	private JTextField logField;
	private JButton dsButton;
	private JButton scriptButton;
	private JButton logButton;
	private JButton execButton;
	private JButton exampleButton;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainWin window = new MainWin();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public MainWin() {
		initialize();
		bind();
		setDefault();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setTitle("脚本批量执行");
		frame.setBounds(100, 100, 471, 266);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		JLabel dsLabel = new JLabel("目标数据源：");
		dsLabel.setFont(new Font("幼圆", Font.BOLD, 15));
		dsLabel.setBounds(35, 39, 102, 24);
		frame.getContentPane().add(dsLabel);
		
		JLabel scriptLabel = new JLabel("执行脚本：");
		scriptLabel.setFont(new Font("幼圆", Font.BOLD, 15));
		scriptLabel.setBounds(35, 87, 102, 24);
		frame.getContentPane().add(scriptLabel);
		
		JLabel logLabel = new JLabel("日志目录：");
		logLabel.setFont(new Font("幼圆", Font.BOLD, 15));
		logLabel.setBounds(35, 135, 102, 24);
		frame.getContentPane().add(logLabel);
		
		dsField = new JTextField();
		dsField.setEditable(false);
		dsField.setBounds(134, 41, 190, 21);
		frame.getContentPane().add(dsField);
		dsField.setColumns(10);
		
		scriptField = new JTextField();
		scriptField.setEditable(false);
		scriptField.setColumns(10);
		scriptField.setBounds(134, 89, 190, 21);
		frame.getContentPane().add(scriptField);
		
		logField = new JTextField();
		logField.setEditable(false);
		logField.setColumns(10);
		logField.setBounds(134, 137, 190, 21);
		frame.getContentPane().add(logField);
		
		dsButton = new JButton("选择");
		dsButton.setBounds(331, 40, 93, 23);
		frame.getContentPane().add(dsButton);
		
		scriptButton = new JButton("选择");
		scriptButton.setBounds(331, 88, 93, 23);
		frame.getContentPane().add(scriptButton);
		
		logButton = new JButton("选择");
		logButton.setBounds(331, 136, 93, 23);
		frame.getContentPane().add(logButton);
		
		execButton = new JButton("执行脚本");
		execButton.setBounds(257, 183, 100, 23);
		frame.getContentPane().add(execButton);
		
		exampleButton = new JButton("使用说明");
		exampleButton.setBounds(114, 183, 100, 23);
		frame.getContentPane().add(exampleButton);
	}
	
	/**
	 * 绑定事件
	 */
	private void bind() {
//		private JButton exampleButton;
		dsButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getPath(dsField, JFileChooser.FILES_ONLY);
			}
		});
		
		scriptButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getPath(scriptField, JFileChooser.DIRECTORIES_ONLY);
			}
		});
		
		logButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getPath(logField, JFileChooser.DIRECTORIES_ONLY);
			}
		});
		
		exampleButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				OpenUrl.openURL("https://gitee.com/lockij/execDDL/blob/master/README.md");
			}
		});
		
		execButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					ExecDDL ddl = new ExecDDL(dsField.getText(), scriptField.getText(), logField.getText());
					ddl.startExec();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		});
	}
	
	/**
	 * 设置默认值
	 */
	private void setDefault() {
		dsField.setText("./datasources.source");
		logField.setText("./log");
		scriptField.setText("./scripts");
	}
	
	/**
	 * 绑定目录选择按钮
	 * 
	 * @param field
	 * @param fileType
	 */
	private void getPath(JTextField field, int fileType) {
		JFileChooser jfc = new JFileChooser(".");
		jfc.setFileSelectionMode(fileType);
		int res = jfc.showOpenDialog(new JFrame());
		if (res == JFileChooser.APPROVE_OPTION) {
			String path = jfc.getSelectedFile().getAbsolutePath();
			// 数据源文件只能选择datasources.source文件
			if (JFileChooser.FILES_ONLY == fileType && !"datasources.source".equals(jfc.getSelectedFile().getName())) {
				return;
			}
			field.setText(path);
		}
	}
}
